package com.woniu.backparking.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.backparking.dto.Cart2CheckDto;
import com.woniu.backparking.dto.PageResult;
import com.woniu.backparking.entity.Admin;
import com.woniu.backparking.entity.Cart;
import com.woniu.backparking.entity.CartCheck;
import com.woniu.backparking.jwtUtil.JwtUtil;
import com.woniu.backparking.service.AdminService;
import com.woniu.backparking.service.CartCheckService;
import com.woniu.backparking.service.CartService;
import com.woniu.backparking.util.BaseEnum;
import com.woniu.backparking.util.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车位审核表 前端控制器
 * </p>
 *
 * @author HashSong
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/cartCheck")
@CrossOrigin
@Slf4j
public class CartCheckController {
    @Resource
    private CartCheckService cartCheckService;
    @Resource
    private AdminService adminService;
    @Resource
    private CartService cartService;
    /*
    查询所有需要验证车位验证申请：
    参数：审核人名字 - 审核状态
    return：只返回需要审核的车位验证申请
     */
    @GetMapping("filterQuery")
    public JSONResult filterQuery(PageResult pageResult){
        log.info("条件查询车位审核申请参数:{}-{}",pageResult.getName01(),pageResult.getState01());
        List<Cart2CheckDto> dtoList = cartCheckService.filterQuery(pageResult);
//        log.info("条件查询车位审核返回值:{}",dtoList);
        return new JSONResult(BaseEnum.StatusCode.Success, BaseEnum.StatusMessage.Success,null,dtoList);
    }
    /*
    对车位发布的处理操作
    参数：处理消息，该条的目标状态，该条id
    return 无
     */
    @PutMapping("check01")
    public JSONResult chache01(CartCheck cartCheck,int cartId,@RequestHeader("token")String token) throws Exception {
        log.info("条件查询车位审核申请参数:内容{}-状态{}-checkId{}-cartId{}",cartCheck.getMessage(),cartCheck.getResult(),cartCheck.getCheckId(),cartId);
        String id= JwtUtil.getId(token);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_id",id);
        Admin one = adminService.getOne(wrapper);
        cartCheck.setAdminName(one.getAdminName());
        if(cartCheck.getResult()==2){
            Cart cart = new Cart();
            cart.setCartId(cartId);
            cart.setAvailable(1);//改为可用 2021.7.13.19.42 1可用，0不可用
            cartService.updateById(cart);
        }
        cartCheckService.updateById(cartCheck);
        return new JSONResult(BaseEnum.StatusCode.Success, BaseEnum.StatusMessage.Success,null,null);
    }
}

